from selenium import webdriver
import time
from selenium.webdriver.common.by import By  # 用于元素定位

# 这段代码作用：关闭 Chrome 的自动化提示（正在受自动化软件的控制）
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ['enable-automation'])

print('开始抢购')
url = 'https://www.apple.com.cn/shop/buy-iphone/iphone-16'
driver = webdriver.Chrome(chrome_options=options)
# 打开地址
driver.get(url)
time.sleep(1)
# 选择
# sku = driver.find_element_by_xpath('//span[@class="column form-selector-left-col rf-bfe-selector-left-col"]/span[contains(text(),""iPhone&nbsp;16"")]')

# sku = driver.find_element_by_xpath('//*[@id=":r7:_label"]/span[1]/span[1]/span/text()')
sku = driver.find_element(By.ID, ':r7:_label')
# sku.click()
# 通过js来模拟点击
driver.execute_script("arguments[0].click();", sku)
time.sleep(1)
driver.find_elements_by_class_name('colornav-item')[3].click()
time.sleep(1)
driver.find_element_by_id(':rg:_label').click()
time.sleep(1)
driver.find_element_by_id('noTradeIn_label').click()
time.sleep(1)
driver.find_element_by_xpath(
    '//*[@id="root"]/div[2]/div[3]/div[3]/div/div/div[1]/div[1]/fieldset/div/div/div[2]').click()
time.sleep(1)
driver.find_element_by_name('add-to-cart').click()
